﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using Remind.IDAL;
using Remind.Model;
using Remind.DBUtility;

namespace Remind.AccessDAL
{
    public class Network:INetwork
    {
        protected const string SQL_SELECT_NETWROK_BY_CID = "SELECT cid, email, qq, msn, blog, website FROM network where cid=@cid";
        protected const string PARM_CID = "@cid";

        protected const string SQL_INSERT_NETWORK = "INSERT INTO network (email, qq, msn, blog, website,cid) VALUES (@email, @qq, @msn, @blog, @website,@cid)";
        #region INetwork 成员

        public NetworkInfo GetNetworkByCid(int cid)
        {
            OleDbParameter parm = new OleDbParameter(PARM_CID, OleDbType.Integer);
            parm.Value = cid;
            using (OleDbDataReader rdr = AccessHelper.ExecuteReader(AccessHelper.ConnectionString, CommandType.Text, SQL_SELECT_NETWROK_BY_CID, parm))
            {
                NetworkInfo network = null;
                if (rdr.Read())
                {
                    network = new NetworkInfo();
                    if (!rdr.IsDBNull(0))
                        network.Cid = rdr.GetInt32(0);
                    if (!rdr.IsDBNull(1))
                        network.Email = rdr.GetString(1);
                    if (!rdr.IsDBNull(2))
                        network.QQ = rdr.GetString(2);
                    if (!rdr.IsDBNull(3))
                        network.Msn = rdr.GetString(3);
                    if (!rdr.IsDBNull(4))
                        network.Blog = rdr.GetString(4);
                    if (!rdr.IsDBNull(5))
                        network.WebSite = rdr.GetString(5);
                }
                return network;
            }
        }

        public int UpdateNetwork(NetworkInfo network, Idu idu)
        {
            int resoult = 0;
            OleDbParameter[] parms = { 
                                       new OleDbParameter("@email",OleDbType.VarChar,128),
                                         new OleDbParameter("@qq",OleDbType.VarChar,128),
                                         new OleDbParameter("@msn",OleDbType.VarChar,128),
                                         new OleDbParameter("@blog",OleDbType.VarChar,128),
                                         new OleDbParameter("@website",OleDbType.VarChar,128),
                                           new OleDbParameter("cid",OleDbType.Integer)
                                     };
            parms[0].Value = network.Email;
            parms[1].Value = network.QQ;
            parms[2].Value = network.Msn;
            parms[3].Value = network.Blog;
            parms[4].Value = network.WebSite;
            parms[5].Value = network.Cid;
            switch (idu)
            {
                case Idu.insert:
                    resoult = AccessHelper.ExecuteNonQuery(AccessHelper.ConnectionString, CommandType.Text, SQL_INSERT_NETWORK, parms);
                    break;
                case Idu.update:
                    break;
                case Idu.delete:
                    break;
                default:
                    break;
            }
            return resoult;
        }

        #endregion
    }
}
